//
//  AppDelegate.m
//  BuyVegetables
//
//  Created by 叶思超 on 15/10/9.
//  Copyright (c) 2015年 叶思超. All rights reserved.
//

#import "AppDelegate.h"
#import "BVHomeViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "DDMenuController.h"
#import "BVLeftMenuController.h"
//＝＝＝＝＝＝＝＝＝＝ShareSDK头文件＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>

//微信SDK头文件
#import "WXApi.h"
//新浪微博SDK头文件
#import "WeiboSDK.h"

// 分享平台的appKey
//#define weixin_appkey          @"wx0c75d865a184ba9f"
//#define weixin_secret          @"0ccb4d2083ad0ad46502af507c36ee3e"
#define weixin_appkey          @"wx6e1d9f7980a2aea8"
#define weixin_secret          @"5d0503713b288d706a461172de3ec19e"

//＝＝＝＝＝＝＝＝＝＝＝＝＝ 百度地图 ＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
#import <BaiduMapAPI/BMapKit.h>
#import "WXApi.h"

// 百度地图key
//原来keyPqZvBjWqp3UcPk2IEEB98fFl"   @“PqZvBjWqp3UcPk2IEEB98fFl”
#define BMKKey @"KTWLNzDAH6BzIgWo8qfO7WDo"

@interface AppDelegate () <BMKGeneralDelegate,WXApiDelegate>
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    // 初始化
    
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    self.window.backgroundColor = [UIColor whiteColor];
    
    // 初始化默认地址信息
    [self configLocation];
    
    // 初始化搜索历史
    [self configSearchHistory];
    // 设置状态栏字体颜色为白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    // shareSDK
    [self setupShareSDK];
    // 百度地图初始化
    [self setupBMK];
    
    // 初始化侧拉菜单
    DDMenuController *rootViewController = [[DDMenuController alloc] initWithRootViewController:[[UINavigationController alloc] initWithRootViewController:[[BVHomeViewController alloc] init]]];
    _menuController = rootViewController;
    
    // 左侧菜单
    BVLeftMenuController *leftMenuVC = [[BVLeftMenuController alloc] init];
    rootViewController.leftViewController = leftMenuVC;
    
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
    
    [WXApi registerApp:weixin_appkey withDescription:@"zuimeiBuyVegetables"];
    
    return YES;
}
/**
 *  初始化搜索历史关键字
 */
- (void)configSearchHistory{
    
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray * array = [defaults objectForKey:@"oldInfo"];
    
    if (!array) {
        [defaults setObject:[NSArray new] forKey:@"oldInfo"];
    }
}
/**
 *  初始化地理信息
 */
- (void)configLocation{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@"山东" forKey:@"provice"];
    [defaults setObject:@"济南" forKey:@"city"];
//    [defaults setObject:@"历下区" forKey:@"dist"];
}
#pragma mark -- 初始化百度地图
- (void)setupBMK {
    BMKMapManager *mageger = [[BMKMapManager alloc] init];
    
    BOOL ret = [mageger start:BMKKey generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    } else {
        NSLog(@"开启成功");
    }
}
#pragma mark -- shareSDK 初始化
- (void)setupShareSDK {
    [ShareSDK registerApp:@"iosv1101"
          activePlatforms:@[@(SSDKPlatformSubTypeWechatTimeline), @(SSDKPlatformSubTypeWechatSession)]
                 onImport:^(SSDKPlatformType platformType) {
                     
                     switch (platformType)
                     {
                         case SSDKPlatformTypeWechat:
                             [ShareSDKConnector connectWeChat:[WXApi class]];
                             break;
                         case SSDKPlatformTypeSinaWeibo:
                             [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                             break;
                         default:
                             break;
                     }
                 }

          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
              
              switch (platformType)
              {
                  case SSDKPlatformTypeSinaWeibo:
                      
                      //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                      [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                                appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                              redirectUri:@"http://www.sharesdk.cn"
                                                 authType:SSDKAuthTypeBoth];
                  case SSDKPlatformTypeWechat:
                      [appInfo SSDKSetupWeChatByAppId:weixin_appkey
                                            appSecret:weixin_secret];
                      break;                      break;
                    default:
                      break;
              }
      }];
}

#pragma mark -- 支付宝支付返回结果
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            
        }];
        return YES;
    }
    return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{

    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        }];
        return YES;
    }

    return [WXApi handleOpenURL:url delegate:self];
}
- (void)onResp:(BaseResp *)resp{
    
    if ([resp isKindOfClass:[PayResp class]]){
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"WXPayResultNotification" object:nil userInfo:@{@"PayResult":resp}];
    }
}

@end
